package com.mybatisflex.example.pojo.entity;

import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

/**
 * <p> 统一的业务返回包装类 </p>
 */
@Data
@Accessors(chain = true)
@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PRIVATE)
public class BizResult<T> implements java.io.Serializable {

    private static final long serialVersionUID = -6939009336549017035L;
    private static final int SUCCESS = 0;
    private T data;
    private String msg;
    private int code;

    /**
     * 正常返回
     *
     * @param data 数据内容
     * @param <T>  返回数据的类型
     * @return 包装后的返回
     */
    public static <T> BizResult<T> create(T data) {
        return BizResult.<T>of().setCode(SUCCESS).setData(data);
    }

    /**
     * 错误返回
     */
    public static <T> BizResult<T> error(int code, String msg) {
        return BizResult.<T>of().setCode(code).setMsg(msg);
    }


}